package com.gangquan360.smartadmin.module.contact.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.anno.DataScope;
import com.gangquan360.smartadmin.common.anno.NoValidPrivilege;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.common.report.BaseExport;
import com.gangquan360.smartadmin.constant.SwaggerTagConst;
import com.gangquan360.smartadmin.module.contact.domain.dto.ContactDTO;
import com.gangquan360.smartadmin.module.contact.domain.dto.ContactQueryDTO;
import com.gangquan360.smartadmin.module.contact.service.ContactService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * [ 联系人表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-07 15:06:41
 * @since JDK1.8
 */
@RestController
@Api(tags = {SwaggerTagConst.Group2.CONTACT_MANGER})
@Slf4j
public class ContactController implements BaseExport {

    @Autowired
    private ContactService contactService;

    @ApiOperation(value = "分页查询联系人表",notes = "@author wiggin")
    @PostMapping("/contact/page/query")
    public ResponseDTO<PageResultDTO<ContactDTO>> queryByPage(@RequestBody ContactQueryDTO queryDTO) {
        return contactService.queryByPage(queryDTO);
    }
    
    @ApiOperation("获取联系人列表")
    @GetMapping("/contact/list")
    @NoValidPrivilege
    public ResponseDTO<List<ContactDTO>> queryList() {
    	return contactService.queryList();
    }
    

    @ApiOperation(value = "添加联系人表",notes = "@author wiggin")
    @PostMapping("/contact/add")
    public ResponseDTO<String> add(@RequestBody @Valid ContactDTO addTO){
        return contactService.add(addTO);
    }

    @ApiOperation(value="修改联系人表",notes = "@author wiggin")
    @PostMapping("/contact/update")
    public ResponseDTO<String> update(@RequestBody @Valid ContactDTO updateDTO){
        return contactService.update(updateDTO);
    }


    @ApiOperation(value="删除联系人表",notes = "@author wiggin")
    @GetMapping("/contact/delete/{id}")
    @DataScope
    public ResponseDTO<String> delete(@PathVariable("id") Long id){
        return contactService.delete(id);
    }


    @ApiOperation(value="详情联系人表",notes = "@author wiggin")
    @GetMapping("/contact/detail/{id}")
    public ResponseDTO<ContactDTO> detail(@PathVariable("id") Long id){
        return contactService.detail(id);
    }

	@Override
	public Page<?> getExportList(Map<String, Object> parameter) {
		ContactQueryDTO queryDTO = JSON.parseObject(JSON.toJSONString(parameter), ContactQueryDTO.class);
		log.info("parameter => {}, queryDTO => {}", parameter, queryDTO);
		queryDTO.setPageNum(1);
		queryDTO.setPageSize(50);
		ResponseDTO<PageResultDTO<ContactDTO>> queryByPage = contactService.queryByPage(queryDTO);
		Page<ContactDTO> page = new Page<ContactDTO>();
		page.setTotal(new Long(queryByPage.getData().getTotal()).intValue());
		page.setRecords(queryByPage.getData().getList());
		return page;
	}
}
